package com.zslc.securitymonitoring.util;

import cn.hutool.json.JSONObject;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;

public class HttpUtil {



    public static JSONObject executePost(String urls, String parame){

        return executePost( urls,  parame,"300000",300000);
    }

    public static JSONObject executePost(String urls, String parame,String str,int s)
    {
        InputStream is = null;
        OutputStream out = null;
        HttpURLConnection http = null;
        try {
            URL url = new URL(urls);
            http = (HttpURLConnection)url.openConnection();
            http.setRequestMethod("POST");
            http.setRequestProperty("Content-Type", "application/json");
            http.setReadTimeout(s);
            http.setDoOutput(true);
            http.setDoInput(true);
            System.setProperty("sun.net.client.defaultConnectTimeout", "30000");
            System.setProperty("sun.net.client.defaultReadTimeout", str);

            http.connect();
            out = http.getOutputStream();
            out.write(parame.getBytes("utf-8"));
            out.flush();
            out.close();

            is = http.getInputStream();
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(is, "utf-8"));
            String currentLine = "";
            StringBuffer message = new StringBuffer("");
            while ((currentLine = bufferedReader.readLine()) != null) {
                message.append(currentLine);
            }
            return new JSONObject(message.toString());
        } catch (Exception e) {
            e.printStackTrace();
            try
            {
                if (is != null) {
                    is.close();
                }
                if (out != null) {
                    out.close();
                }
                if (http != null)
                    http.disconnect();
            }
            catch (IOException e1) {
                e1.printStackTrace();
            }
        }
        return null;
    }



    public static JSONObject validate(String urls, String parame,String userToken)
    {
        InputStream is = null;
        OutputStream out = null;
        HttpURLConnection http = null;
        try {
            URL url = new URL(urls);
            http = (HttpURLConnection)url.openConnection();
            http.setRequestMethod("POST");
            http.setRequestProperty("Content-Type", "application/json");
            http.setDoOutput(true);
            http.setDoInput(true);
            http.setRequestProperty("user_token",userToken);
            System.setProperty("sun.net.client.defaultConnectTimeout", "300000");
            System.setProperty("sun.net.client.defaultReadTimeout", "300000");

            http.connect();
            out = http.getOutputStream();
            out.write(parame.getBytes("utf-8"));
            out.flush();
            out.close();

//            is = http.getInputStream();
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(http.getInputStream(), "utf-8"));
            String currentLine = "";
            StringBuffer message = new StringBuffer("");
            while ((currentLine = bufferedReader.readLine()) != null) {
                message.append(currentLine);
            }
            System.out.println("权限校验接口 :"+urls+" \n "+parame);
            System.out.println("权限校验结果 :"+message.toString());
            return new JSONObject(message.toString());
        } catch (Exception e) {
            e.printStackTrace();
            try
            {
                if (is != null) {
                    is.close();
                }
                if (out != null) {
                    out.close();
                }
                if (http != null)
                    http.disconnect();
            }
            catch (IOException e1) {
                e1.printStackTrace();
            }
        }
        return null;
    }


}
